<include file="Public/header"/>
<include file="Public/menu"/>

<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            商品
            <small>管理</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 商品管理</a></li>
            <li class="active">商品分类</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">添加分类</h3>

                        <div class="box-tools">

                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="/admin/storeCat/index" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp;分类列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="/admin/storeCat/add" method="post" id="form" class="form-horizontal" pjax-container="">
                        <div class="box-body">
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">分类级别</label>
                                <div class="col-sm-6">
                                    <select class="form-control  select2-hidden-accessible level" style="width: 100%;" name="level">
                                        <option value="1">一级分类</option>
                                        <option value="2">二级分类</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">导航菜单</label>
                                <div class="col-sm-6">
                                    <select class="form-control  select2-hidden-accessible parent_id" style="width: 100%;" name="parent_id" disabled="disabled">
                                        <option value="">请选择</option>
                                        <?php foreach($parent as $v) {?>
                                        <option value="<?php echo $v['store_cat_id']; ?>"><?php echo $v['cat_name']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group 1">
                                <label class="col-sm-2 control-label">分类名称</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-pencil"></i></span>
                                        <input type="test" name="cat_name" value="" class="form-control" placeholder="分类名称">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group 1">
                                <label for="image" class="col-sm-2 control-label">类别图片标识</label>
                                <div class="col-sm-6">
                                    <input type="file" id="image" data-max-file-count="1" />
                                    <input type="hidden" id="image_action"/>
                                </div>
                            </div>
                            <div for="image" class="form-group 1">
                                <label class="col-sm-2 control-label"></label>
                                <div class="col-sm-6 suggest-1" style="margin-left: 0px; color: #da2424">* 建议一级分类宽高474px*149px</div>
                                <div class="col-sm-6 suggest-2" style="margin-left: 0px; color: #da2424; display: none;">* 二级分类宽高126px*126px</div>
                            </div>
                            <!--<div class="form-group 1">-->
                                <!--<label for="image" class="col-sm-2 control-label">类别背景图片标识</label>-->
                                <!--<div class="col-sm-6">-->
                                    <!--<input type="file" id="cat_bg_img"  data-max-file-count="1" />-->
                                    <!--<input type="hidden" id="cat_bg_img_action"/>-->
                                <!--</div>-->
                            <!--</div>-->

                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <div class="col-sm-2">
                            </div>
                            <div class="col-sm-6">
                                <div class="btn-group pull-left">
                                    <button id='hahah' type="submit" class="btn btn-info pull-right">提交</button>
                                </div>
                            </div>

                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>

            </div>
        </div>

    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<!--上传插件-->
<link rel="stylesheet" href="/Public/admin/fileinput/fileinput.min.css">
<script src="/Public/admin/fileinput/fileinput.min.js"></script>
<script data-exec-on-popstate>

    $(function () {

        $('.level').change(function () {
            var num = $(this).val();
            if (num == 2) {
                $('.parent_id').removeAttr('disabled');
                $('.suggest-1').hide();
                $('.suggest-2').show();
            }else {
                $('.parent_id').attr('disabled','disabled');
                $('.suggest-2').hide();
                $('.suggest-1').show();
            }
        });

        // 上传图片js
        $("#image").fileinput({
            overwriteInitial: true,
            dropZoneEnabled: false,
            uploadUrl: '/admin/upload/index/',
            language: 'zh',
            showRemove: false,
            maxFileCount: 1,
            browseClass: 'btn btn-primary btn-docup',
            showPreview: true,
            uploadClass: 'btn btn-default btn-docup',
            allowedFileTypes: ["image"],
            browseLabel: "添加图片",
            uploadLabel: "上传图片",

        }).on("fileuploaded", function (event, data, previewId, index) {
            var response = data.response;
            if (data.jqXHR.status == 200) {
                // ajax 成功返回数据写入
                $('.file-preview-frame').eq(index).append("<input class='abc' type='hidden' name='cat_img[]' value='"+response+"'>");
            }
            if (response.info != undefined ){
                alert(response.info);
            }
        });

        $("#image").on('filecleared', function(event) {
            $("#image_action").val(1);
        });

    //    $("#cat_bg_img").fileinput({"overwriteInitial":true,"showUpload":false,"language":"en","allowedFileTypes":["image"],"initialCaption":""});

        $("#cat_bg_img").fileinput({
            overwriteInitial: true,
            dropZoneEnabled: false,
            uploadUrl: '/admin/upload/index/"',
            language: 'zh',
            showRemove: false,
            maxFileCount: 1,
            browseClass: 'btn btn-primary btn-docup',
            showPreview: true,
            uploadClass: 'btn btn-default btn-docup',
            allowedFileTypes: ["image"],
            browseLabel: "添加图片",
            uploadLabel: "上传图片",

        }).on("fileuploaded", function (event, data, previewId, index) {
            var response = data.response;
            if (data.jqXHR.status == 200) {
                // ajax 成功返回数据写入
                $('.file-preview-frame').eq(index).append("<input class='abc' type='hidden' name='cat_bg_img[]' value='"+response+"'>");
            }
        });
        $("#cat_bg_img").on('filecleared', function(event) {
            $("#cat_bg_img_action").val(1);
        });

        $('.grid-select-all').change(function() {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });

        $('.batch-delete').on('click', function() {
            var selected = [];
            $('.grid-item:checked').each(function(){
                selected.push($(this).data('id'));
            });

            if (selected.length == 0) {
                return;
            }

            if(confirm("Are you sure to delete this item ?")) {
                $.post('/admin/auth/roles/' + selected.join(), {_method:'delete','_token':'ALpaP7hXZ6s2dnAdE02Z6mUSHPt8RkYbcDRp6rpw'}, function(data){
                    $.pjax.reload('#pjax-container');
                    noty({
                        text: "<strong>Succeeded!</strong>",
                        type:'success',
                        timeout: 1000
                    });
                });
            }
        });
        $("#hahah").submit();
//        $('#hahah').on('click',function(e){
//
//            e.preventDefault();
//            $('.file-preview-frame').each(function(){
////                console.log('提交'+$(this).data('url'));
//                console.log($('.abc').val());
//            })
//        })

        //表单验证
        $("#form").validate({
            rules: {
                cat_name:{
                    required: true
                }
            },
            messages:{
                cat_name: {
                    required: "请输入分类名称",
                }
            },
            submitHandler: function(form){
                form.submit();   //提交表单
            }
        });

    });
</script>